package com.tiger.cloud.service.crm;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tiger.cloud.common.exception.ServiceException;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.dao.crm.UserConsumeDao;
import com.tiger.cloud.enums.UserConsumer;
import com.tiger.cloud.enums.UserConsunmeSource;
import com.tiger.cloud.model.crm.UserAccount;
import com.tiger.cloud.model.crm.UserConsume;
import com.tiger.cloud.util.ErrorCode;

/**
 * 
 */
@Service("userConsumeService")
public class UserConsumeService extends TigerBaseService<UserConsume> implements IUserConsumeService{
    
    @Autowired
	private UserConsumeDao userConsumeDao;
    @Autowired
    private IUserAccountService userAccountService;

	@Override
	public MybatisTigerDao<UserConsume> getEntityDao() {
		return userConsumeDao;
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public UserConsume insertRecord(String pointCode,Long userId,String remark) {
		Integer point=CommonPointRule.getPoint(pointCode);
		UserConsume userConsunme = new UserConsume();
		userConsunme.setUserId(userId);
		userConsunme.setRemark(remark);
		userConsunme.setType(UserConsumer.GIVEPOINT.getCode());
		userConsunme.setAmount(new BigDecimal(point));
		userConsunme.setSource(UserConsunmeSource.PRESENTGIVE.getCode());
		if(userConsumeDao.insert(userConsunme)>0){
			UserAccount userAccount=new UserAccount();
			userAccount.setIntegral(point+0l);
			userAccount.setUserId(userId);
			userAccount.setBalance(new BigDecimal(point));
			if(userAccountService.updateByUserId(userAccount)>0){
				return userConsunme;
			}else{
				throw new ServiceException(ErrorCode.UPDATE_USER_ACCOUNT_ERROR);
			}
		}else{
			throw new ServiceException(ErrorCode.INSERT_USER_CONSUME_ERROR);
		}
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public UserConsume insertRecord(String pointCode,Long userId,Integer type, Integer source,String remark) {
		Integer point=CommonPointRule.getPoint(pointCode);
		UserConsume userConsunme = new UserConsume();
		userConsunme.setUserId(userId);
		userConsunme.setRemark(remark);
		userConsunme.setType(type);
		userConsunme.setAmount(new BigDecimal(point));
		userConsunme.setSource(source);
		if(userConsumeDao.insert(userConsunme)>0){
			UserAccount userAccount=new UserAccount();
			userAccount.setIntegral(point+0l);
			userAccount.setUserId(userId);
			userAccount.setBalance(new BigDecimal(point));
			if(userAccountService.updateByUserId(userAccount)>0){
				return userConsunme;
			}else{
				throw new ServiceException(ErrorCode.UPDATE_USER_ACCOUNT_ERROR);
			}
		}else{
			throw new ServiceException(ErrorCode.INSERT_USER_CONSUME_ERROR);
		}
	}
	
}